package com.travelshare.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.travelshare.common.annotation.Log;
import com.travelshare.common.core.controller.BaseController;
import com.travelshare.common.core.domain.AjaxResult;
import com.travelshare.common.core.domain.entity.SysDept;
import com.travelshare.common.enums.BusinessType;
import com.travelshare.business.domain.InfoCommunity;
import com.travelshare.business.service.IInfoCommunityService;
import com.travelshare.common.utils.SecurityUtils;
import com.travelshare.common.utils.poi.ExcelUtil;
import com.travelshare.framework.config.ServerConfig;

/**
 * 社区信息Controller
 * 
 * @author travelshare
 * @date 2024-02-22
 */
@RestController
@RequestMapping("/business/infoCommunity")
public class InfoCommunityController extends BaseController
{
    @Autowired
    private IInfoCommunityService infoCommunityService;
    
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询社区信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:infoCommunity:list')")
    @GetMapping("/list")
    public AjaxResult list(InfoCommunity infoCommunity)
    {
    	infoCommunity.setUserId(getUserId());
        List<InfoCommunity> list = infoCommunityService.selectInfoCommunityList(infoCommunity);
        return success(list);
    }

    /**
     * 导出社区信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:infoCommunity:export')")
    @Log(title = "社区信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoCommunity infoCommunity)
    {
        List<InfoCommunity> list = infoCommunityService.selectInfoCommunityList(infoCommunity);
        ExcelUtil<InfoCommunity> util = new ExcelUtil<InfoCommunity>(InfoCommunity.class);
        util.exportExcel(response, list, "社区信息数据");
    }

    /**
     * 获取社区信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:infoCommunity:query')")
    @GetMapping(value = "/{communityId}")
    public AjaxResult getInfo(@PathVariable("communityId") Long communityId)
    {
        return success(infoCommunityService.selectInfoCommunityByCommunityId(communityId));
    }

    /**
     * 新增社区信息
     */
    @PreAuthorize("@ss.hasPermi('business:infoCommunity:add')")
    @Log(title = "社区信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoCommunity infoCommunity)
    {
    	infoCommunity.setUserId(getUserId());
    	infoCommunity.setDelFlag("0");
    	infoCommunity.setCreateBy(getUsername());
    	infoCommunity.setClickNum(0);
        return toAjax(infoCommunityService.insertInfoCommunity(infoCommunity));
    }

    /**
     * 修改社区信息
     */
    @PreAuthorize("@ss.hasPermi('business:infoCommunity:edit')")
    @Log(title = "社区信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoCommunity infoCommunity)
    {
    	if(!SecurityUtils.hasRole("admin")) {
    		if(infoCommunity.getUserId().longValue() != getUserId()) {
    			return error("无权限编辑该内容");
    		}
    	}
    	infoCommunity.setUpdateBy(getUsername());
        return toAjax(infoCommunityService.updateInfoCommunity(infoCommunity));
    }

    /**
     * 删除社区信息
     */
    @PreAuthorize("@ss.hasPermi('business:infoCommunity:remove')")
    @Log(title = "社区信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{communityIds}")
    public AjaxResult remove(@PathVariable Long[] communityIds)
    {
    	if(communityIds == null) {
    		return error("请选择删除内容");
    	}
    	if(!SecurityUtils.hasRole("admin")) {
    		Integer count = infoCommunityService.checkCommunityByCommunityIdsAndUserId(communityIds, getUserId());
    		if(count != communityIds.length) {
    			return error("删除内容包含他人信息，您无权删除！");
    		}
    	}
        return toAjax(infoCommunityService.deleteInfoCommunityByCommunityIds(communityIds));
    }
    
    @GetMapping("/getParentList")
    public AjaxResult getParentList(InfoCommunity infoCommunity)
    {
    	infoCommunity.setParentId(0l);
    	infoCommunity.setUserId(getUserId());
        List<InfoCommunity> list = infoCommunityService.selectInfoCommunityList(infoCommunity);
        if(!list.isEmpty()) {
        	String url = serverConfig.getUrl() + "/profile";
        	for(InfoCommunity cm : list) {
        		if(StringUtils.isNotBlank(cm.getContent()) && cm.getContent().contains("/profile")) {
        			cm.setContent(cm.getContent().replaceAll("/profile" , url));
            	}
            	if(StringUtils.isNotBlank(cm.getPicPath()) && cm.getPicPath().contains("/profile")) {
            		cm.setPicPath(cm.getPicPath().replaceAll("/profile" , url));
            	}
            	if(StringUtils.isNotBlank(cm.getAvatar()) && cm.getAvatar().contains("/profile")) {
            		cm.setAvatar(cm.getAvatar().replaceAll("/profile" , url));
            	}
        	}
        }
        return success(list);
    }
    
    @GetMapping("/getList/{communityId}")
    public AjaxResult getList(@PathVariable("communityId") Long communityId)
    {
        List<InfoCommunity> list = infoCommunityService.selectChildCommunityListById(communityId, getUserId());
        if(!list.isEmpty()) {
        	String url = serverConfig.getUrl() + "/profile";
        	for(InfoCommunity cm : list) {
        		if(StringUtils.isNotBlank(cm.getContent()) && cm.getContent().contains("/profile")) {
        			cm.setContent(cm.getContent().replaceAll("/profile" , url));
            	}
            	if(StringUtils.isNotBlank(cm.getPicPath()) && cm.getPicPath().contains("/profile")) {
            		cm.setPicPath(cm.getPicPath().replaceAll("/profile" , url));
            	}
            	if(StringUtils.isNotBlank(cm.getAvatar()) && cm.getAvatar().contains("/profile")) {
            		cm.setAvatar(cm.getAvatar().replaceAll("/profile" , url));
            	}
        	}
        }
        return success(list);
    }
    
    @GetMapping("/communityTree/{communityId}")
    public AjaxResult communityTree(@PathVariable("communityId") Long communityId)
    {
    	String url = serverConfig.getUrl() + "/profile";
        return success(infoCommunityService.selectCommunityTreeList(communityId, getUserId(), url));
    }
}
